<?php
function LongToShortLink_showShortLinks($target, $mother) {
	$target = preg_replace('#\<a href="(http:\/\/[^"]+)"(.*?)\>(.*?)\<\/a>#ie', 'LongToShortLink_handle_url_tag(\'$1\',\'$3\',\'$2\')', $target);
	return $target;
}

function LongToShortLink_handle_url_tag($url, $link='', $option='')
{
	global $configVal;
	requireComponent('Textcube.Function.misc');
	$data = misc::fetchConfigVal($configVal);
	if(!is_null($data)){
		$checkURL = $data['checkURL'];
		$leftURL = $data['leftURL'];
		$rightURL = $data['rightURL'];
	} else {
		$checkURL	= 55; // 체크할 URL 길이
		$leftURL	= 39; // '...'의 왼쪽에 나타낼 URL 길이
		$rightURL	= 8; // '...'의 오른쪽에 나타낼 URL의 길이
	}

	$full_url = str_replace(array(' ', '\'', '`', '"'), array('%20', '', '', ''), $url);
	$url  = "http://".htmlspecialchars($url);
	$url = str_replace("http://http://","http://",strtolower($url));

	$templink = strip_tags($link);
	$templink = "http://".htmlspecialchars($templink);
	$templink = str_replace("http://http://","http://",strtolower($templink));

	if (strpos($url, 'www.') === 0)
		$full_url = 'http://'.$full_url;
	else if (strpos($url, 'ftp.') === 0)
		$full_url = 'ftp://'.$full_url;
	else if (!preg_match('#^([a-z0-9]{3,6})://#', $url, $bah))
		$full_url = 'http://'.$full_url;

	$link = ($templink == '' || $templink == $url) ? ((strlen($url) > $checkURL) ? UTF8::lessenAsEm($url, $leftURL, '').' &middot;&middot;&middot; '.substr(rawurlencode($url), ($rightURL*-1)) : stripslashes($link)) : stripslashes($link);

	if($option){
		$option = str_replace('\"','"',$option);
		$link = '<a href="'.$full_url.'" '.$option.'>'.$link.'</a>';
	}else{
		$link = '<a href="'.$full_url.'">'.$link.'</a>';
	}
	return $link;
}

function LongToShortLink_DataSet($data){
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($data);
	return true;
}
?>
